package network.UDPTest02;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpReceiveDemo02 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6667);

        while(true){

            //接受包
            byte[] container = new byte[20];
            DatagramPacket packet = new DatagramPacket(container, 0, container.length);

            socket.receive(packet);

            //断开连接  bye
            byte[] data = packet.getData();
            String receiveData = new String(data, 0, data.length);
            System.out.println(receiveData);

            //发送bye时，跳出循环，即断开连接
            if(receiveData.equals("bye")){
                break;
            }
        }

        socket.close();
    }
}
